package com.eastfair.certificate.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.builder.ConstructionPersonnelFeign;
import com.eastfair.builder.dto.ConstructionPersonnelDTO;
import com.eastfair.builder.enumeration.ConstructionPersonnelGenderEnum;
import com.eastfair.builder.enumeration.ConstructionPersonnelWorkTypeEnum;
import com.eastfair.builder.vo.ConstructionPersonnelVO;
import com.eastfair.certificate.dto.*;
import com.eastfair.certificate.entity.CertificateIndex;
import com.eastfair.certificate.entity.People;
import com.eastfair.certificate.service.PeopleService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.vo.CertificateIndexVO;
import com.eastfair.certificate.vo.CertificateUserVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.database.mybatis.typehandler.TypeHandlerUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import com.eastfair.venueservice.dto.ServiceProviderDTO;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 人员库
 * </p>
 *
 * @author ligang
 * @date 2023-03-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/people")
@Api(value = "People", tags = "人员库")
@PreAuth(replace = "certificate:people:")
public class PeopleController extends SuperController<PeopleService, Long, People, PeoplePageQuery, PeopleSaveDTO, PeopleUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ServiceProviderFeign serviceProviderFeign;
    @Autowired
    ConstructionPersonnelFeign constructionPersonnelFeign;


    @Override
    public void handlerResult(IPage<People> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
        List<People> peopleList = page.getRecords();
        if (CollectionUtil.isNotEmpty(peopleList)) {
            List<Long> companyIds = peopleList.stream().map(People::getCompanyId).collect(Collectors.toList());
            //查询服务商信息
            ServiceProviderDTO query = new ServiceProviderDTO();
            query.setIds(companyIds);
            R<List<ServiceProviderVO>> serviceProviderListR = serviceProviderFeign.queryServiceProvider(query);
            if (serviceProviderListR.getIsSuccess() && serviceProviderListR.getData() != null && !serviceProviderListR.getData().isEmpty()) {
                for (ServiceProviderVO serviceProviderVO : serviceProviderListR.getData()) {
                    for (People people : peopleList) {
                        if (serviceProviderVO.getId().equals(people.getCompanyId())) {
                            people.setCompanyName(serviceProviderVO.getCompanyName());
                        }
                    }
                }
            }
        }
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<People> peopleList = list.stream().map((map) -> {
            People people = People.builder().build();
            //TODO 请在这里完成转换
            return people;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(peopleList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<PeoplePageQuery> params) {
        PeoplePageQuery pageQuery = params.getModel();
        //加密处理
//        if(StringUtils.isNotBlank(pageQuery.getCardNumber())){
//            pageQuery.setCardNumber(TypeHandlerUtil.crypt(pageQuery.getCardNumber()));
//        }
//        if(StringUtils.isNotBlank(pageQuery.getContactInformation())){
//            pageQuery.setContactInformation(TypeHandlerUtil.crypt(pageQuery.getContactInformation()));
//        }
    }

    /**
     * 证件指标分页
     *
     * @param params 证件指标实体
     * @return
     */
    @ApiOperation(value = "证件指标分页", notes = "证件指标分页")
    @PostMapping("/pageVo")
    public R<IPage<People>> pageVo(@RequestBody PageParams<People> params) {
        IPage<People> page = baseService.pageVo(params);
        handlerResult(page);
        return R.success(page);
    }


    /**
     * 查询搭建商员工信息
     *
     * @param peopleDTO 证件指标实体
     * @return
     */
    @ApiOperation(value = "查询搭建商员工信息", notes = "查询搭建商员工信息")
    @PostMapping("/querypersonnel")
    public R<List<People>> pageVo(@RequestBody PeopleDTO peopleDTO) {
        List<People> result = new ArrayList<>();
        ConstructionPersonnelDTO constructionPersonnelDTO = new ConstructionPersonnelDTO();
        constructionPersonnelDTO.setExhibitionId(peopleDTO.getExhibitionId());
        constructionPersonnelDTO.setBuilderId(peopleDTO.getCompanyId());
        constructionPersonnelDTO.setCardNumber(peopleDTO.getCardNumber());
        R<List<ConstructionPersonnelVO>> listR = constructionPersonnelFeign.queryConstructionPersonnelByExhibition(constructionPersonnelDTO);
        if(listR.getIsSuccess()){
            List<ConstructionPersonnelVO> constructionPersonnelVOList = listR.getData();
            if(CollectionUtil.isNotEmpty(constructionPersonnelVOList)){
                for(ConstructionPersonnelVO constructionPersonnelVO : constructionPersonnelVOList){
                    People people = new People();
                    people.setFullName(constructionPersonnelVO.getName());
                    if(constructionPersonnelVO.getGender()!=null){
                        if(ConstructionPersonnelGenderEnum.MALE.eq(constructionPersonnelVO.getGender())){
                            people.setGender("MALE");
                        }else{
                            people.setGender("FEMALE");
                        }
                    }
                    //MALE  FEMALE
                    people.setCardNumber(constructionPersonnelVO.getCardNumber());
                    if(ConstructionPersonnelWorkTypeEnum.HIGH_ALTITUDE_WORK_PERMIT.eq(constructionPersonnelVO.getWorkType())){
                        people.setWorkType("HIGH_ALTITUDE_WORK");
                    }else if(ConstructionPersonnelWorkTypeEnum.ELECTRICIAN_CERT.eq(constructionPersonnelVO.getWorkType())){
                        people.setWorkType("TEST_PANEL");
                    }else if(ConstructionPersonnelWorkTypeEnum.OTHER.eq(constructionPersonnelVO.getWorkType())){
                        people.setWorkType("OTHER");
                    }
                    people.setCompanyId(constructionPersonnelVO.getBuilderId());
                    people.setCompanyName(constructionPersonnelVO.getBuilderName());
                    result.add(people);
                }
            }
        }
        return R.success(result);
    }



}
